package com.zsn.demo.test;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicReferenceArray;

/**
 * @author zsn
 * @CreateTime: 2022/11/27 21:42
 * @Description:
 */
public class volatileTest2 {
    public static void main(String[] args) {

        //在 main 线程中创建 10 个子线程
        for (int i = 0; i < 100; i++) {
            new MyThread().start();
        }
    }

    static class MyThread extends Thread {
        public volatile static int count;
//        public static AtomicInteger count = new AtomicInteger();


        public  static void addCount() {
            for (int i = 0; i < 1000; i++) {
                //count++不是原子操作
                count++;
            }
            System.out.println(Thread.currentThread().getName() + " count=" + count);
        }
//        static void addCount() {
//            for (int i = 0; i < 1000; i++) {
//                count.getAndIncrement();
//            }
//            System.out.println(Thread.currentThread().getName() + " count=" + count.get());
//        }

        @Override
        public void run() {
            addCount();
        }
    }

}